<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="部门" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.department`" :rules="formRules.department" class="mb-0px!">
            <el-input v-model="row.department" placeholder="请输入部门" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="期望到期时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.eTime`" :rules="formRules.eTime" class="mb-0px!">
            <el-date-picker
              v-model="row.eTime"
              type="date"
              value-format="x"
              placeholder="选择期望到期时间"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="单据日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.documentDate`" :rules="formRules.documentDate" class="mb-0px!">
            <el-date-picker
              v-model="row.documentDate"
              type="date"
              value-format="x"
              placeholder="选择单据日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
       <el-table-column label="条码" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.barCode`" :rules="formRules.barCode" class="mb-0px!">
            <el-input v-model="row.barCode" placeholder="请输入条码" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productType`" :rules="formRules.productType" class="mb-0px!">
            <el-select v-model="row.productType" placeholder="请选择商品类型">
                <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="类别" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.category`" :rules="formRules.category" class="mb-0px!">
            <el-input v-model="row.category" placeholder="请输入类别" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="货物名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.gname`" :rules="formRules.gname" class="mb-0px!">
            <el-input v-model="row.gname" placeholder="请输入货物名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="型号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.model`" :rules="formRules.model" class="mb-0px!">
            <el-input v-model="row.model" placeholder="请输入型号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="品牌" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.brand`" :rules="formRules.brand" class="mb-0px!">
            <el-input v-model="row.brand" placeholder="请输入品牌" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="单位" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.unit`" :rules="formRules.unit" class="mb-0px!">
            <el-input v-model="row.unit" placeholder="请输入单位" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.number`" :rules="formRules.number" class="mb-0px!">
            <el-input v-model="row.number" placeholder="请输入数量" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="主要技术参数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.technicalParameter`" :rules="formRules.technicalParameter" class="mb-0px!">
            <el-input v-model="row.technicalParameter" placeholder="请输入主要技术参数" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.notes`" :rules="formRules.notes" class="mb-0px!">
            <el-input v-model="row.notes" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="申请人" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.applicantName`" :rules="formRules.applicantName" class="mb-0px!">
            <el-input v-model="row.applicantName" placeholder="请输入申请人" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="申请日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.applicationDate`" :rules="formRules.applicationDate" class="mb-0px!">
            <el-date-picker
              v-model="row.applicationDate"
              type="date"
              value-format="x"
              placeholder="选择申请日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="部门负责审核人" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.bexamine`" :rules="formRules.bexamine" class="mb-0px!">
            <el-input v-model="row.bexamine" placeholder="请输入部门负责审核人" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="部门审核日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.bexamineDate`" :rules="formRules.bexamineDate" class="mb-0px!">
            <el-date-picker
              v-model="row.bexamineDate"
              type="date"
              value-format="x"
              placeholder="选择部门审核日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="仓库管理审核人" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.cexamine`" :rules="formRules.cexamine" class="mb-0px!">
            <el-input v-model="row.cexamine" placeholder="请输入仓库管理审核人" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="仓库审核日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.cexamineDate`" :rules="formRules.cexamineDate" class="mb-0px!">
            <el-date-picker
              v-model="row.cexamineDate"
              type="date"
              value-format="x"
              placeholder="选择仓库审核日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="批号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.batchNumber`" :rules="formRules.batchNumber" class="mb-0px!">
            <el-input v-model="row.batchNumber" placeholder="请输入批号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="是否进入出库流程" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.isOut`" :rules="formRules.isOut" class="mb-0px!">
            <el-input v-model="row.isOut" placeholder="请输入是否进入出库流程" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="部门审核不通过原因" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.bexamineNo`" :rules="formRules.bexamineNo" class="mb-0px!">
            <el-input v-model="row.bexamineNo" placeholder="请输入部门审核不通过原因" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="仓库审核不通过原因" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.cexamineNo`" :rules="formRules.cexamineNo" class="mb-0px!">
            <el-input v-model="row.cexamineNo" placeholder="请输入仓库审核不通过原因" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="规格" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.specif`" :rules="formRules.specif" class="mb-0px!">
            <el-input v-model="row.specif" placeholder="请输入规格" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="1—备货" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.bh`" :rules="formRules.bh" class="mb-0px!">
            <el-input v-model="row.bh" placeholder="请输入1—备货" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加办公申请单</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { MaterialDemandApi } from '@/api/werp/purchase/materialdemand'

const props = defineProps<{
  documentNumber: undefined // 单据编号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.documentNumber,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await MaterialDemandApi.getOfficeOrderListByDocumentNumber(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    department: undefined,
    eTime: undefined,
    documentDate: undefined,
    documentNumber: undefined,
    barCode: undefined,
    productType: undefined,
    category: undefined,
    gname: undefined,
    model: undefined,
    brand: undefined,
    unit: undefined,
    number: undefined,
    technicalParameter: undefined,
    notes: undefined,
    applicantName: undefined,
    applicationDate: undefined,
    bexamine: undefined,
    bexamineDate: undefined,
    cexamine: undefined,
    cexamineDate: undefined,
    batchNumber: undefined,
    isOut: undefined,
    bexamineNo: undefined,
    cexamineNo: undefined,
    specif: undefined,
    bh: undefined,
  }
  row.documentNumber = props.documentNumber
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>
